/*******************************************************************************
 * Copyright (c) 2008 Sylvain Sicard.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the GNU Lesser Public License v2.1
 * which accompanies this distribution, and is available at
 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
 * 
 * Contributors:
 *     Sylvain Sicard - initial API and implementation
 ******************************************************************************/
package fr.jade.resources.joram.wrapper;

import java.util.Iterator;
import java.util.Set;

import org.objectweb.fractal.api.Component;
import org.objectweb.fractal.api.Interface;
import org.objectweb.fractal.api.NoSuchInterfaceException;
import org.objectweb.fractal.api.control.LifeCycleController;
import org.objectweb.joram.client.jms.admin.AdminException;

import fr.jade.resources.joram.api.JoramElements;
import fr.jade.resources.joram.api.UserInterface;
import fr.jade.util.CollectionUtil;
import fr.jade.util.FractalNavigator;
import fr.jade.util.FractalUtil;


public class JoramUtils {
	
	
	public static final int PORT_NUMBER_START = 16020;
	
	public static Set<Component> getAllServers(Component myself) throws NoSuchInterfaceException, AdminException{

		return FractalNavigator.nameStartWithFilter(FractalNavigator.getSiblingOrSelf(getServerComponent(myself)),"JoramServer_");
	}
	
	public static Component getServerComponent(Component myself) throws NoSuchInterfaceException, AdminException {

		try{
			return (Component)CollectionUtil.checkAndGetSingleton(FractalNavigator.nameStartWithFilter(FractalNavigator.getParents(myself), "JoramServer_"));
		}catch(IndexOutOfBoundsException e){
			throw new AdminException("Unable to find the server wrapper");
		}
	}
	
	public static UserInterface getUserInterfaceFromName(Component myself, String name) throws Exception{
		return (UserInterface)getInterfaceInAllServers(myself, JoramElements.user,name);
	}
	
	public static Interface getInterfaceInAllServers(Component myself, JoramElements itfType,String name) throws NoSuchInterfaceException, AdminException{
		Interface res = null;
		Iterator<Component> it = getAllServers(myself).iterator();
		while(it.hasNext()){
			res = getInterfaceFromServerCompAndName(it.next(),name,itfType);
			if(res != null){
				return res;
			}
		}
		throw new AdminException("Unable to find the component interface in domain");
	}
	
	public static Interface getInterfaceFromServerCompAndName(Component server, String name, JoramElements itfType) throws NoSuchInterfaceException{
		
		String itfName = null;
		if (itfType == JoramElements.topic) {
			itfName = "topic";
		} else if (itfType == JoramElements.queue) {
			itfName = "queue";
		} else if (itfType == JoramElements.user) {
			itfName = "user";
		}
		
		Component cmp = FractalNavigator.getChildByName(server, name);
		
		return (Interface)cmp.getFcInterface(itfName);
	}

	public static boolean serverIsStarted(Component cmp) {
    try{
      Component server = getServerComponent(cmp);
      String state = FractalUtil.getLifeCycleController(server).getFcState();
      return state.equals(LifeCycleController.STARTED);
    }catch(Exception e){
      return true;
    }
  }
}
